package andytest;

import static org.junit.Assert.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import uk.ac.glasgow.clui.SystemCommandException;
import uk.ac.glasgow.demosocs.Candidate;
import uk.ac.glasgow.demosocs.ElectionResult;
import uk.ac.glasgow.demosocs.ElectionState;
import uk.ac.glasgow.demosocs.impl.election.ElectionImpl;
import uk.ac.glasgow.demosocs.impl.ui.DateUtil;
import uk.ac.glasgow.demosocs.users.Electorate;

public class electiontest  {

	ElectionImpl election,election2;

	@Before
	public void setUp() throws Exception {
		election = new ElectionImpl("name1", "type1");
		election2 = new ElectionImpl("name2", "type2");
	}

	@After
	public void tearDown() throws Exception {
	}

	
	@Test
	/**
	 * general test for electionimpl create GUID1 GUID2 GUID3
	 * and make voting and check the result
	 */
	/*
	public void testSerious() {

		election.setElectionID("id1");
		// id setting right
		assertSame("id1", election.getEID());
		Electorate ele = new Electorate("electorate1");
		ele.addUser("surname1", "GUID1", "forename1", "password1");
		ele.addUser("surname2", "GUID2", "forename2", "password2");
		ele.addUser("surname3", "GUID3", "forename3", "password3");

		
		election.setElectorate(ele);
		// ele test
		assertSame(election.getElectorate(), ele);
		assertSame(election.getElectionState(), ElectionState.NOT_STARTED);
		// NOMINATE GUID2
		election.openNomination();

		election.nominate("GUID1", "GUID2");
		election.nominate("GUID2", "GUID3");

		assertSame(election.getElectionState(), ElectionState.NOMINATIONS_OPEN);
		assertTrue(election.isNomineeSetComtainsGUID("GUID2"));
		// accept nomination
		election.nomineeAccept("GUID2");
		election.nomineeAccept("GUID3");
		
		// is guid2 is a candidate
		assertTrue(election.isCandidateSetComtainsGUID("GUID2"));

		// change to voting process
		election.openElection();
		assertSame(election.getElectionState(), ElectionState.STARTED);
		// vote guid2
		election.vote("GUID1", "GUID2");
		// TEST GUID2 VOTES
		assertTrue(election.getCandidateByGUID("GUID2").getVotes() == 1);
		
		//vote another
		election.vote("GUID1", "GUID3");
		assertTrue(election.getCandidateByGUID("GUID2").getVotes() == 0);//first vote should be canceled
		assertTrue(election.getCandidateByGUID("GUID3").getVotes() == 1);//candidate guid3 has one vote

		//guid2 vote for guid3
		election.vote("GUID2", "GUID3");
		assertTrue(election.getCandidateByGUID("GUID3").getVotes() == 2);//candidate guid3 has TWO vote

		//guid2 change vote
		election.vote("GUID2", "GUID2");
		assertTrue(election.getCandidateByGUID("GUID2").getVotes() == 1);//candidate guid2 has one vote

		//election finished
		election.endElection();
		assertSame(election.getElectionState(), ElectionState.ENDED);
	
		//check the result
		ElectionResult result = election.getElectionResult();
		for(Candidate can : result.getWinningCandidates())
		System.out.println(can.getGUID());
		
		
	}

	*/
	public void testDate() throws SystemCommandException, ParseException{
		election2.setElectionID("id2");
		assertSame("id2", election2.getEID());

		Electorate ele = new Electorate("electorate1");
		ele.addUser("surname1", "GUID1", "forename1", "password1");
		ele.addUser("surname2", "GUID2", "forename2", "password2");
		ele.addUser("surname3", "GUID3", "forename3", "password3");

		election2.setElectorate(ele);

		SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
		
		election2.setNominationOpenDate(format.parse("2012/02/12"));
		election2.setStart(format.parse("2012/02/13"));
		election2.setEnd(format.parse("2012/02/14"));
		//System.out.println(openNominations.before(new Date()));
		//System.out.println(election2.getOpenNominations());
		election2.checkTime();
		System.out.println(election2.getElectionState());
	}
}
